﻿namespace Web
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using WebCamera;

    public class PageCamera
    {
        private List<BrowserPageProxy> _ProxyList = new List<BrowserPageProxy>();

        public void Attach()
        {
            this._ProxyList = BrowserPageFactory.GetAllPages();
        }

        public void Attach(bool activePageOnly)
        {
            if (activePageOnly)
            {
                this._ProxyList.Clear();
                BrowserPageProxy activePage = BrowserPageFactory.GetActivePage();
                if (activePage != null)
                {
                    this._ProxyList.Add(activePage);
                }
            }
            else
            {
                this._ProxyList = BrowserPageFactory.GetAllPages();
            }
        }

        public void Attach(IntPtr MainWindowHandle)
        {
            this._ProxyList = new List<BrowserPageProxy>();
        }

        public void Attach(string URL)
        {
            this._ProxyList = BrowserPageFactory.GetAllPages(URL);
        }

        public void Attach(string URL, bool openInNewPage)
        {
            if (openInNewPage)
            {
                this._ProxyList = new List<BrowserPageProxy>();
            }
            else
            {
                this.Attach(URL);
            }
        }

        public void Shoot()
        {
            this.Shoot(Configration.SavePath);
        }

        public void Shoot(string savePath)
        {
            int num = 0;
            string path = Path.Combine(Path.GetDirectoryName(savePath), Path.GetFileNameWithoutExtension(savePath));
            foreach (BrowserPageProxy proxy in this._ProxyList)
            {
                bool flag = false;
                if (proxy.Visible)
                {
                    flag = true;
                }
                else
                {
                    proxy.Visible = true;
                }
                int num2 = 0;
                while ((num2 * proxy.ScreenHeight) < proxy.Height)
                {
                    proxy.Y = (proxy.ScreenHeight - proxy.BrowserWindow.Edge) * num2;
                    num2++;
                }
                num2--;
                for (int i = 0; (i * proxy.ScreenWidth) < proxy.Width; i++)
                {
                    proxy.X = (proxy.ScreenWidth - proxy.BrowserWindow.Edge) * i;
                    for (int j = num2; j >= 0; j--)
                    {
                        proxy.Y = (proxy.ScreenHeight - proxy.BrowserWindow.Edge) * j;
                        proxy.PrintWindow();
                    }
                }
                if (!flag)
                {
                    proxy.Visible = false;
                }
                if (++num > 1)
                {
                    path = string.Concat(new object[] { path, "-", num, Path.GetExtension(savePath) });
                }
                else
                {
                    path = savePath;
                }
                proxy.CombineImage(path);
            }
        }

        public void ShootWithoutScroll(string savePath)
        {
            //Console.WriteLine(this._ProxyList.Count);
            int num = 0;
            string destFileName = Path.Combine(Path.GetDirectoryName(savePath), Path.GetFileNameWithoutExtension(savePath));
            foreach (BrowserPageProxy proxy in this._ProxyList)
            {
                bool flag = false;
                if (proxy.Visible)
                {
                    flag = true;
                }
                else
                {
                    proxy.Visible = true;
                }
                string str3 = proxy.PrintWindow();
                if (!flag)
                {
                    proxy.Visible = false;
                }
                if (++num > 1)
                {
                    destFileName = string.Concat(new object[] { destFileName, "-", num, Path.GetExtension(savePath) });
                }
                else
                {
                    destFileName = savePath;
                }
                if (!string.IsNullOrEmpty(str3))
                {
                    File.Copy(str3, destFileName, true);
                }
            }
        }
    }
}

